package com.richard.dev.common;

import androidx.fragment.app.FragmentManager;

import com.richard.dev.common.databinding.DialogTestBinding;
import com.richard.library.basic.basic.BasicBindingDialogFragment;
import com.richard.library.basic.util.LogUtil;
import com.richard.library.simplerx.XObservableOnSubscribe;
import com.richard.library.simplerx.XSubscribe;
import com.richard.library.simplerx.concurrent.ConcurrentCallback;
import com.richard.library.simplerx.concurrent.ExecuteTask;
import com.richard.library.simplerx.concurrent.XObservableConcurrent;

import java.util.List;

/**
 * @author: Richard
 * @createDate: 2024/10/14 11:22
 * @version: 1.0
 * @description: none
 */
public class TestDialog extends BasicBindingDialogFragment<DialogTestBinding> {

    public static void start(FragmentManager manager){
        TestDialog dialog = new TestDialog();
        dialog.showSingle(manager);
    }

    @Override
    public void initLayoutView() {
        setContentView(R.layout.dialog_test);
    }

    @Override
    public void initData() {
        XObservableConcurrent.create(new XObservableOnSubscribe<List<ExecuteTask>>() {
                    @Override
                    public List<ExecuteTask> run() throws Throwable {
                        return List.of(new ExecuteTask("test1", (ExecuteTask.TRRunnable<String, Boolean>) taskData -> {
                            Thread.sleep(1000);
                            return true;
                        }),new ExecuteTask("test2", (ExecuteTask.TRRunnable<String, Boolean>) taskData -> {
                            Thread.sleep(1000);
                            return true;
                        }),new ExecuteTask("test3", (ExecuteTask.TRRunnable<String, Boolean>) taskData -> {
                            Thread.sleep(1000);
                            return true;
                        }));
                    }
                })
                .execute(false, new ConcurrentCallback() {
                    @Override
                    public void onSubTaskFinish(ExecuteTask task) {
                        LogUtil.dTag("testtt","===========" + task.getName());
                    }
                })
                .bindLife(this)
                .toAsyncSubscribe(new XSubscribe<List<ExecuteTask>>() {
                    @Override
                    public void onXNext(List<ExecuteTask> data) {
                        getUIView().showMsg("finish");
                    }

                    @Override
                    public void onXError(Throwable e) {
                        getUIView().showErrorMsg(e.getMessage());
                    }
                });
    }

    @Override
    public void bindListener() {

    }
}
